package com.muyaxuan.mail.dao;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.muyaxuan.mail.common.PageModel;
import com.muyaxuan.mail.dto.Goods;
import com.muyaxuan.mail.dto.GoodsType;

public class GoodsDao extends BaseDao{
	public static final String NAME_SPACE="com.muyaxuan.mail.dto.Goods.";
	/**
	 * 根据上架时间获取商品
	 * @return
	 */
    public List<Goods> getGoodsByDate(){
		//获取会话
		SqlSession session = getSqlSession();
		//接收数据库返回的数据
		List<Goods> goodsList = session.selectList(NAME_SPACE+"getGoodsByDate");
		
		closeSqlSession();
		//返回商品
		return goodsList;
	}
    /**
	 * 根据售价获取商品
	 * @return
	 */
    public List<Goods> getGoodsByPirce(){
		//获取会话
		SqlSession session = getSqlSession();
		//接收数据库返回的数据
		List<Goods> goodsList = session.selectList(NAME_SPACE+"getGoodsByPrice");
		
		closeSqlSession();
		//返回商品
		return goodsList;
	}
	
	/**
	 * 根据code获取商品
	 * @return
	 */
    public Goods getGoodsByCode(String code){
		//获取会话
		SqlSession session = getSqlSession();
		//接收数据库返回的数据
		Goods goods = session.selectOne(NAME_SPACE+"getGoodsByCode",code);
		
		closeSqlSession();
		//返回商品
		return goods;
	}
	
	/**
	 * 获取商品总条数
	 * @return
	 */
	public int count(String title){
		
		
		SqlSession session = getSqlSession();
		
		Goods goods = new Goods();
		
		goods.setTitle(title);

		int count = session.selectOne(NAME_SPACE+"count",goods);
		
		closeSqlSession();
		
		return count;
	}
	/**
	 * 分页查询商品
	 * @return
	 */
    public List<Goods> getGoodsByPage(Map<String,Object> params){
		//获取会话
		SqlSession session = getSqlSession();
		//接收数据库返回的数据
		List<Goods> goods = session.selectList(NAME_SPACE+"getGoodsByPage",params);
		
		closeSqlSession();
		//返回集合
		return goods;
	}
    /**
	 * 根据id获取商品
	 * @return
	 */
    public Goods getGoodsById(int id){
		//获取会话
		SqlSession session = getSqlSession();
		//接收数据库返回的数据
		Goods goods = session.selectOne(NAME_SPACE+"getGoodsById",id);
		
		closeSqlSession();
		//返回商品
		return goods;
	}
    /**
	 * 更新商品类型
	 * @return
	 */
    public String updateGoodsType(GoodsType goodsType){
		//获取会话
		SqlSession session = getSqlSession();
		//接收数据库返回的数据
		int count = session.update(NAME_SPACE+"update",goodsType);
		
		session.commit();
		
		closeSqlSession();
		//tip
		return count>0?"更新成功":"更新失败";
	}
    /**
	 * 新增商品
	 * @return
	 */
    public String addGoods(Goods goods){
		//获取会话
		SqlSession session = getSqlSession();
		//接收数据库返回的数据
		int count = session.insert(NAME_SPACE+"addGoods",goods);
		
		session.commit();
		
		closeSqlSession();
		//tip
		return count>0?"新增成功":"新增失败";
	}
    /**
	 * 更新商品
	 * @return
	 */
    public String updateGoodsById(Goods goods){
		//获取会话
		SqlSession session = getSqlSession();
		//接收数据库返回的数据
		int count = session.insert(NAME_SPACE+"updateGoodsById",goods);
		
		session.commit();
		
		closeSqlSession();
		//tip
		return count>0?"新增成功":"新增失败";
	}

    /**
	 * 删除商品
	 * @return
	 */
    public void deleteGoods(int id){
		//获取会话
		SqlSession session = getSqlSession();
		//接收数据库返回的数据
		session.delete(NAME_SPACE+"deleteGoods",id);
		
		session.commit();
		
		closeSqlSession();
	}

}
